package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.constant.DeviceConstant;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.dto.device.DmcEditDeviceDto;
import com.csun.cmny.provider.model.dto.rfid.BindDmcRfidLabelOldmanDto;
import com.csun.cmny.provider.model.dto.rfid.RfidLabelPageQuery;
import com.csun.cmny.provider.model.enums.device.DmcDeviceTypeEnum;
import com.csun.cmny.provider.model.vo.OldmanBedRoomBuildingVo;
import com.csun.cmny.provider.model.vo.OpcUserVo;
import com.csun.cmny.provider.model.vo.rfid.DmcRfidLabelVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcDeviceService;
import com.csun.cmny.provider.service.DmcRfidLabelOldmanService;
import com.csun.cmny.provider.service.OmcRpcService;
import com.csun.cmny.provider.service.UacRpcService;
import com.csun.cmny.util.PubUtils;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.RedisKeyUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-01-08 13:25
 * @Description: the class dmc device rfid label controller.
 */
@RestController
@RequestMapping(value = "/rfidLabel", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcRfidLabelController extends BaseController {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private OmcRpcService omcRpcService;
    @Resource
    private UacRpcService uacRpcService;

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private DmcDeviceService dmcDeviceService;

    @Resource
    private DmcRfidLabelOldmanService dmcRfidLabelOldmanService;

    /**
     * 查询组织门禁标签设备列表
     *
     * @param rfidLabelPageQuery   the rfid label page query
     *
     * @return  the wrapper
     */
    @ApiOperation(httpMethod = "POST", value = "查询组织门禁标签设备列表")
    //@LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @RequestMapping(value = "/queryGroupRfidLabelListWithPage", method = RequestMethod.POST)
    public Wrapper queryGroupRfidLabelListWithPage(@RequestBody RfidLabelPageQuery rfidLabelPageQuery) {

        rfidLabelPageQuery.setGroupId(getLoginAuthDto().getGroupId());
        rfidLabelPageQuery.setDeviceType(DmcDeviceTypeEnum.RFID_LABEL.getIndex());

        PageInfo pageInfo = dmcRfidLabelOldmanService.queryRfidLabelListWithPage(rfidLabelPageQuery);

        for (Object o : pageInfo.getList()) {
            DmcRfidLabelVo dmcRfidLabelVo = (DmcRfidLabelVo) o;
            if (!PubUtils.isNull(dmcRfidLabelVo.getRoleId())) {
                dmcRfidLabelVo.setBind(true);
                switch (dmcRfidLabelVo.getRoleCode()) {
                    case DeviceConstant.RFID_ROLE_CODE_OLDMAN :
                        OldmanBedRoomBuildingVo obv = (OldmanBedRoomBuildingVo) redisTemplate
                                .opsForValue().get(RedisKeyUtil.getOmcOldmanId(dmcRfidLabelVo.getOldmanId().intValue()));
                        if (PublicUtil.isNotEmpty(obv) && PublicUtil.isNotEmpty(obv.getOldmanName())) {
                            dmcRfidLabelVo.setOldmanName(obv.getOldmanName());
                        }
                        break;
                    case DeviceConstant.RFID_ROLE_CODE_EMP :
                        OpcUserVo opcUserVo = uacRpcService.queryOpcUserVoByUserId(dmcRfidLabelVo.getOldmanId());
                        if (PublicUtil.isNotEmpty(opcUserVo) && PublicUtil.isNotEmpty(opcUserVo.getUserName())) {
                            dmcRfidLabelVo.setOldmanName(opcUserVo.getUserName());
                        }
                        break;
                    case DeviceConstant.RFID_ROLE_CODE_OTHER :
                        break;
                }

            }
        }

       // return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    /**
     * 添加门禁标签设备
     *
     * @param dmcEditDeviceDto  the dmc edit device dto
     * @param br                the binding result
     *
     * @return  the wrapper
     */
    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "添加门禁标签设备")
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Wrapper addRfidLabel(@Validated @RequestBody DmcEditDeviceDto dmcEditDeviceDto, BindingResult br) {

        DmcDevice dmcDevice = new DmcDevice();

        BeanUtils.copyProperties(dmcEditDeviceDto, dmcDevice);
        dmcDevice.setDeviceType(DmcDeviceTypeEnum.RFID_LABEL.getIndex());

        Integer result = dmcDeviceService.addDevice(dmcDevice, getLoginAuthDto());

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 修改门禁标签设备
     *
     * @param dmcEditDeviceDto  the dmc edit device dto
     * @param br                the binding result
     *
     * @return  the wrapper
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @ApiOperation(httpMethod = "POST", value = "修改门禁标签设备")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Wrapper editRfidLabel(@Validated @RequestBody DmcEditDeviceDto dmcEditDeviceDto,
                                 BindingResult br) {

        DmcDevice dmcDevice = new DmcDevice();
        BeanUtils.copyProperties(dmcEditDeviceDto, dmcDevice);
        dmcDevice.setDeviceType(DmcDeviceTypeEnum.RFID_LABEL.getIndex());

        Integer result = dmcDeviceService.editDevice(dmcDevice, getLoginAuthDto());

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 根据Id删除门禁标签
     *
     * @param id    the id
     *
     * @return  the wrapper
     */
    @ApiOperation(httpMethod = "POST", value = "根据Id删除门禁标签")
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.POST)
    public Wrapper deleteDmcRfidLabelById(@PathVariable Long id) {

        Integer result = dmcDeviceService.deleteDmcDeviceById(id);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 绑定门禁卡标签 - 老人
     */
    @ValidateAnnotation
    //@LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @RequestMapping(value = "/bindDmcRfidLabelOldman", method = RequestMethod.POST)
    public Wrapper bindDmcRfidLabelOldman(@Validated @RequestBody BindDmcRfidLabelOldmanDto bindDmcRfidLabelOldmanDto,
                                          BindingResult br) {

        Integer result = dmcRfidLabelOldmanService.bindDmcRfidLabelOldman(bindDmcRfidLabelOldmanDto);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }


    /**
     * 根据门禁卡标签Id解绑
     */
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @RequestMapping(value = "/breakDmcRfidLabelOldmanByDeviceId/{deviceId}", method = RequestMethod.POST)
    public Wrapper breakDmcRfidLabelOldmanByDeviceId(@PathVariable Long deviceId) {

        Integer result = dmcRfidLabelOldmanService.breakDmcRfidLabelOldmanByDeviceId(deviceId);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 根据老人Id解绑
     */
    //@LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @RequestMapping(value = "/breakDmcRfidLabelOldmanByOldmanId/{oldmanId}", method = RequestMethod.POST)
    public Wrapper breakDmcRfidLabelOldmanByOldmanId(@PathVariable Integer oldmanId) {

        Integer result = dmcRfidLabelOldmanService.breakDmcRfidLabelOldmanByOldmanId(oldmanId);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }
}
